﻿//刷新网页时数据任然存在
$(document).ready(function () {
    showData();
})
function addEvent() {
    var thing = $("#list");
    if (thing.val() == "") {
        alert("事件内容不能为空");
        return;
    }
    var event = {"thing": thing.val(),"Done":false };
    var data = getData();
    data.push(event);
    saveData(data);
    showData();
    thing.val("");
}
function getData() {
    var data = localStorage.getItem("eventList");
    if (data != null) {
        return JSON.parse(data);
    }
    else {
        return [];
    }
}
function saveData(data) {
    localStorage.setItem("eventList", JSON.stringify(data));
}
function completeEvent(index) {
    var data = getData();
    if (data[index].Done)
        data[index].Done = false;
    else
       data[index].Done = true;
    saveData(data);
    showData();
}
function deleteEvent(index) {
    var data = getData();
    data.splice(index, 1);
    saveData(data);
    showData();
}
function clearEvent() {
    localStorage.clear();
    showData();
}
function showData() {
  
    var todoList = $("#eventList");
    var doneList = $("#overEvent");
    var number1 = $("#todoList");
    var number2 = $("#doneList");
    var listThings = localStorage.getItem("eventList");
    if (listThings != null) {
        var data = JSON.parse(listThings);
        var todoCount = 0;
        var doneCount = 0;
        var todoStr = "";
        var doneStr = ""; 
        for (var i = 0; i < data.length; i++) {
            //事件已完成  
          //  alert("#事件内容不能为空");
            if (data[i].Done) {
                doneStr += "<li class=\"todo\"><input class=\"boolBox\" type=\"checkbox\" checked=\"checked\" onclick=\"completeEvent(" + i + ")\" />"
                    + data[i].thing + "<a onclick=\"deleteEvent(" + i + ")\">-</a></li>";
                doneCount++;
            }
            //事件未完成
            else {
                todoStr += "<li class=\"done\"><input class=\"boolBox\" type=\"checkbox\" onclick=\"completeEvent(" + i + ")\" />"
                    + data[i].thing + "<a onclick=\"deleteEvent(" + i + ")\">-</a></li>";
                todoCount++;
            }
        };
        number1.html(todoCount);
        todoList.html(todoStr);
      number2.html(doneCount);
      doneList.html(doneStr);
    }
    else {
        number1.html(0);
        todoList.html("");
        number2.html (0);
        doneList.html("");
    }
}
